#include "dac.h"

#define SPI_MASTER_Tx_DMA_FLAG_TC       DMA1_FLAG_TC1  
#define SPI_MASTER_Tx_DMA_FLAG_TE       DMA1_FLAG_TE1  
#define SPI_MASTER_Tx_DMA_FLAG_HT       DMA1_FLAG_HT1  
#define SPI_MASTER_Tx_DMA_FLAG_GL      DMA1_FLAG_GL1  

uint16_t I2S_DAC_Buffer[4];

void Internal_DacInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	DAC_InitTypeDef DAC_InitStructure;
	
	/* GPIOA Periph clock enable */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	/* DAC Periph clock enable */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);

  /* Once the DAC channel is enabled, the corresponding GPIO pin is automatically 
     connected to the DAC converter. In order to avoid parasitic consumption, 
     the GPIO pin should be configured in analog */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4 | GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	/* DAC channel1 Configuration */
  //DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software;
	DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
  DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
  //DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_2047;
	DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095;
  DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
  DAC_Init(DAC_Channel_1, &DAC_InitStructure);

  /* DAC channel2 Configuration */
  //DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1023;
  DAC_Init(DAC_Channel_2, &DAC_InitStructure);

  /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is 
     automatically connected to the DAC converter. */
  DAC_Cmd(DAC_Channel_1, ENABLE);

  /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is 
     automatically connected to the DAC converter. */
  DAC_Cmd(DAC_Channel_2, ENABLE);

  /* Set DAC dual channel DHR12RD register */
  //DAC_SetDualChannelData(DAC_Align_12b_R, 0x100, 0x100);
	
	/* Set DAC Channel1 DHR12L register */
  DAC_SetChannel1Data(DAC_Align_12b_R, 0x01FF);
	DAC_SetChannel2Data(DAC_Align_12b_R, 0x07FF);
}

void I2S_DacInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	I2S_InitTypeDef I2S_InitStructure;
	DMA_InitTypeDef    DMA_InitStructure;
	
	//SPI3 clocks enable
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); 
	
	//GPIO enable
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB 
											 | RCC_APB2Periph_GPIOC , ENABLE);
	//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	
	SPI_I2S_DeInit(SPI2);
	
	//GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);
	//GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable , ENABLE);
	
	//Configure SPI2 pins: CK, WS and SD 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	//I2S peripheral configuration
	//I2S_Cmd(SPI3, DISABLE);
	
  I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
	//I2S_InitStructure.I2S_Standard = I2S_Standard_MSB;
  I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_24b;
  I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Enable;
	//I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
  I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_32k;
  I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;
	I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
		
  //I2S3 configuration
  I2S_Init(SPI2, &I2S_InitStructure);
	
	//I2S_Cmd(SPI2, ENABLE);
	
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
	
	DMA_ClearFlag(SPI_MASTER_Tx_DMA_FLAG_TC | SPI_MASTER_Tx_DMA_FLAG_TE | SPI_MASTER_Tx_DMA_FLAG_HT | SPI_MASTER_Tx_DMA_FLAG_GL);
	DMA_Cmd(DMA1_Channel5, DISABLE);
	DMA_DeInit(DMA1_Channel5);
  //DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)0x4001300C;	//SPI_MASTER_DR_Base
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)0x4000380C;
	//DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)0x4000300C;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)I2S_DAC_Buffer;
	//Specifies if the peripheral is the source or destination.
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  DMA_InitStructure.DMA_BufferSize = 4;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	//Specifies whether the memory address register is incremented or not.
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	//Specifies the operation mode of the DMAy Channelx.
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
	//DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel5, &DMA_InitStructure);
	
	/* Enable SPI_MASTER DMA Tx request */
  SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
	
	DMA_Cmd(DMA1_Channel5, ENABLE);
	
	//Enable the I2S3
  I2S_Cmd(SPI2, ENABLE);

	
	I2S_DACSendData(VPP_INIT_VAL,OFFSET_INIT_VAL);
	//I2S_DACSendData(OFFSET_INIT_VAL,VPP_INIT_VAL);
	
	/*GPIO_InitTypeDef GPIO_InitStructure;
	I2S_InitTypeDef I2S_InitStructure;
	
	//SPI3 clocks enable
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE); 
	
	//GPIO enable
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB 
											 | RCC_APB2Periph_GPIOC , ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	
	SPI_I2S_DeInit(SPI3);
	
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);
	//GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable , ENABLE);
	
	
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	
	//I2S peripheral configuration
	//I2S_Cmd(SPI3, DISABLE);
	
  I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
  I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_24b;
  I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Enable;
	//I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
  I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_32k;
  I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;
	I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
		
  //I2S3 configuration
  I2S_Init(SPI3, &I2S_InitStructure);

	//Enable the I2S3
  I2S_Cmd(SPI3, ENABLE);*/

}


void I2S_DACSendData(u32 datL, u32 datR)
{
	I2S_DAC_Buffer[0]=((datL >> 8) & 0xffff);
	I2S_DAC_Buffer[1]=((datL & 0xff) << 8);
	I2S_DAC_Buffer[2]=((datR >> 8) & 0xffff);
	I2S_DAC_Buffer[3]=((datR & 0xff) << 8);
}
/*void I2S_DACSendData(u32 datL, u32 datR)
{

	while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
	SPI_I2S_SendData(SPI2, ((datL >> 8) & 0xffff));
	while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
	SPI_I2S_SendData(SPI2, ((datL & 0xff) << 8));
	while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
	SPI_I2S_SendData(SPI2, ((datR >> 8) & 0xffff));
	while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
	SPI_I2S_SendData(SPI2, ((datR & 0xff) << 8));
}*/
